package com.groupbuying.settleproducer.service.impl;

import com.groupbuying.settleproducer.connector.FinancialFeign;
import com.groupbuying.settleproducer.constants.SettleUshopWithdrawalYsBalancePaymentEnumConstants;
import com.groupbuying.settleproducer.dao.SettleUshopWithdrawalYsBalancePaymentDao;
import com.groupbuying.settleproducer.domain.SettleUshopWithdrawalYsBalancePaymentDO;
import com.groupbuying.settleproducer.service.SettleUshopBalancePaymentService;
import com.groupbuying.settleproducer.utils.DateUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/8/30 14:23
 * @Des 金融中心余额代付
 */
@Service
public class SettleUshopBalancePaymentServiceImpl implements SettleUshopBalancePaymentService {
    public static final Logger logger = LoggerFactory.getLogger(SettleUshopBalancePaymentServiceImpl.class);
    @Autowired
    private FinancialFeign financialFeign;
    @Autowired
    private SettleUshopWithdrawalYsBalancePaymentDao settleUshopWithdrawalYsBalancePaymentDao;
    @Override
    public Boolean createBalancePayment(Map<String, Object> map) {
        Map<String,Object> financialMap =new HashMap<>();
        /*获取参数调用金融中心*/
        logger.info("SettleUshopBalancePaymentServiceImpl.createBalancePayment()-----一清+余额代付从商户中心获取到的map信息:" + map.toString());
        // 解析map参数---商户号（门店id）
        Object tempParam = map.get("storeUuid");
        String storeUuid = tempParam == null ? null : tempParam.toString();

        // 解析map参数---代付金额
        tempParam = map.get("balanceAmount");
        String balancePaymentAmountStr = tempParam == null ? null : tempParam.toString();
        BigDecimal balancePaymentAmount = new BigDecimal(balancePaymentAmountStr);
        //获取代付id（从商户中心获取）
        tempParam = map.get("withdrawalId");
        String withdrawalId = tempParam == null ? null : tempParam.toString();
        /*生成随机数4位*/
        String mathString = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
        String outTradeNo = DateUtils.format(new Date(), "yyyyMMdd") + storeUuid + mathString;
        // 解析map参数---订单说明
        tempParam = map.get("subject");
        String subject = tempParam == null ? null : tempParam.toString();
        /*-----------获取该商户的银行卡信息---------*/
        // 解析map参数---银行名称
        tempParam = map.get("bankName");
        String bankName = tempParam == null ? null : tempParam.toString();
        // 解析map参数---开户行所在城市
        tempParam = map.get("cityName");
        String bankCity = tempParam == null ? null : tempParam.toString();
        // 解析map参数---银行帐号
        tempParam = map.get("bankAccountNo");
        String bankAccountNo = tempParam == null ? null : tempParam.toString();
        // 解析map参数---银行帐号用户名
        tempParam = map.get("bankAccountName");
        String bankAccountName = tempParam == null ? null : tempParam.toString();
        // 解析map参数---收款方银行账户类型corporate :对公账户;personal:对私账户
        tempParam = map.get("bankAccountType");
        String bankAccountType = tempParam == null ? null : tempParam.toString();
        // 解析map参数---支持卡类型debit:借记卡;credit:信用卡 unit:单位结算卡
        tempParam = map.get("bankCardType");
        String bankCardType = tempParam == null ? null : tempParam.toString();
        //判断请求参数不为空
        if (StringUtils.isEmpty(balancePaymentAmountStr) || StringUtils.isEmpty(withdrawalId) || StringUtils.isEmpty(outTradeNo) || StringUtils.isEmpty(subject) || StringUtils.isEmpty(bankName) || StringUtils.isEmpty(bankCity) ||
                StringUtils.isEmpty(bankAccountNo) || StringUtils.isEmpty(bankAccountName) || StringUtils.isEmpty(bankAccountType) || StringUtils.isEmpty(bankCardType)
        ) {
            logger.info("SettleUshopBalancePaymentServiceImpl.createBalancePayment()-----从store中心获取的数据可能存在空数据:" + map.toString());
            return false;
        }
        /*组装参数请求金融中心*/
        financialMap.put("outTradeNo",outTradeNo);
        financialMap.put("balancePaymentAmount",balancePaymentAmountStr);
        financialMap.put("bankName",bankName);
        financialMap.put("subject",subject);
        financialMap.put("bankCity",bankCity);
        financialMap.put("bankAccountName",bankAccountName);
        financialMap.put("bankCardType",bankCardType);
        financialMap.put("bankAccountType",bankAccountType);
        financialMap.put("bankAccountNo",bankAccountNo);
        logger.info("SettleUshopBalancePaymentServiceImpl.createBalancePayment()-----请求银盛前余额代付参数信息:" + financialMap.toString());
        Boolean status = financialFeign.balancePayment(financialMap);
        logger.info("SettleUshopBalancePaymentServiceImpl.createBalancePayment()-----请求银盛余额代付参数返回boolean为:" + status);
        /*余额记录子表初始化*/
        /*记录余额代付子表记录*/
        SettleUshopWithdrawalYsBalancePaymentDO settleUshopWithdrawalYsBalancePaymentDO =new SettleUshopWithdrawalYsBalancePaymentDO();
        try {
            settleUshopWithdrawalYsBalancePaymentDO.setGmtCreate(new Date());
            settleUshopWithdrawalYsBalancePaymentDO.setGmtModified(new Date());
            settleUshopWithdrawalYsBalancePaymentDO.setIsDelete(SettleUshopWithdrawalYsBalancePaymentEnumConstants.ENUM_ISDELETE_UNDEL);
            settleUshopWithdrawalYsBalancePaymentDO.setStoreId(storeUuid);
            settleUshopWithdrawalYsBalancePaymentDO.setOutTradeNo(outTradeNo);
            settleUshopWithdrawalYsBalancePaymentDO.setWithdrawalId(withdrawalId);
            settleUshopWithdrawalYsBalancePaymentDO.setWithdrawalPrice(balancePaymentAmount);
            settleUshopWithdrawalYsBalancePaymentDO.setSplitState(SettleUshopWithdrawalYsBalancePaymentEnumConstants.ENUM_SPLITSTATE_FAIL);
            settleUshopWithdrawalYsBalancePaymentDao.save(settleUshopWithdrawalYsBalancePaymentDO);
            logger.info("SettleUshopBalancePaymentServiceImpl.createBalancePayment()-----银盛余额代付子表保存成功!");
            if(status){
                /*更改余额记录子表为成功状态*/
                Map<String,Object> condMap =new HashedMap();
                condMap.put("withdrawalId",withdrawalId);
                condMap.put("splitState",SettleUshopWithdrawalYsBalancePaymentEnumConstants.ENUM_SPLITSTATE_SUC);
                settleUshopWithdrawalYsBalancePaymentDao.updateStatus(condMap);
                logger.info("SettleUshopBalancePaymentServiceImpl.createBalancePayment()-----余额子表落库成功，返回提现总表记录成功！");
                return true;
            }else{
                logger.info("SettleUshopBalancePaymentServiceImpl.createBalancePayment()-----余额子表初始化失败");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
